import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:toast/toast.dart';
import 'package:wanflutter/ui/page/page_collect_add_website.dart';

import '../../http/api.dart';

class WebsiteCollectPage extends StatefulWidget {

  @override
  State<WebsiteCollectPage> createState() => _WebsiteCollectPageState();
}

class _WebsiteCollectPageState extends State<WebsiteCollectPage>
    with AutomaticKeepAliveClientMixin {
  bool _isHidden = false;

  List _collects = [];


  @override
  void initState() {
    super.initState();
    _getCollects();
  }

  _getCollects() async{
    var result = await Api.getWebSiteCollects();
    if(result != null) {
      var data = result['data'];
      _collects.clear();
      _collects.addAll(data);
      _isHidden = true;
      setState(() {
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Offstage(
          offstage: _isHidden,
          child: Center(
            child: CircularProgressIndicator(),
          ),
        ),
        Offstage(
          ///无内容时显示文本
          offstage: _collects.isNotEmpty || !_isHidden,
          child: Center(
            child: Text("(＞﹏＜) 你还没有收藏任何内容......"),
          ),
        ),
        Offstage(
          ///网络数据为空则隐藏
          offstage: _collects.isEmpty,
          child: RefreshIndicator(
            onRefresh: ()=> _getCollects(),
            child: ListView.separated(
                physics: AlwaysScrollableScrollPhysics(),
                padding: EdgeInsets.all(22.0),
                itemBuilder: (context, i) => _buildItem(context, i),
                separatorBuilder: (context, i) {
                  return Padding(
                    padding: EdgeInsets.only(top: 8.0, bottom: 8.0),
                    child: Divider(color: Colors.grey,),
                  );
                },
                itemCount: _collects.length),
          ),
        ),

        ///定位
        Positioned(
          bottom: 18.0,
            right: 18.0,
            //悬浮按钮
            child: FloatingActionButton(
              child: Icon(Icons.add),
              onPressed: _addCollect,
            )
        )
      ],
    );
  }

  _addCollect() async {
    var result = await Navigator.push(
      context, MaterialPageRoute(builder: (_) => WebsiteAddPage())
    );
    if (result != null) {
      _collects.add(result);
    }
  }

  _buildItem(BuildContext context, int i){
    var item = _collects[i];
    ///侧滑删除
    return Slidable(
      ///控制action的现实方式？delegate已弃用
      // delegate: SlidableDrawerDelegate(),
      // 右侧的action
      startActionPane: ActionPane(
        extentRatio: 0.25,
        motion: DrawerMotion(),
        children: [
          SlidableAction(
              onPressed: _delCollect(item),
              backgroundColor: Colors.red,
              foregroundColor: Colors.white,
              icon: Icons.delete,
              label: 'Delete',
          )
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(item['name'], style: TextStyle(fontSize: 22.0),),
          Padding(
            padding: EdgeInsets.only(top: 8.0),
            child: Text(
              item['link'],
              style: TextStyle(color: Theme.of(context).primaryColor),
            ),
          ),
        ],
      ),
    );
  }

  _delCollect(item) async {
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (_) {
          return Center(
            child: CircularProgressIndicator(),
          );
        });
    // 移除收藏接口
    var result = await Api.unCollectWebsite(item['id']);
    Navigator.pop(context);
    if (result['errorCode'] != 0) {
      Toast.show(result['errorCode'], gravity: Toast.center);
    } else {
      setState(() {
        _collects.remove(item);
      });
    }
  }

  @override
  bool get wantKeepAlive => true;
}
